/**
 * 
 */
package br.unesp.ibb.lbbc.fba;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.commons.math.optimization.GoalType;
import org.apache.commons.math.optimization.OptimizationException;
import org.apache.commons.math.optimization.RealPointValuePair;
import org.apache.commons.math.optimization.linear.LinearConstraint;
import org.apache.commons.math.optimization.linear.LinearObjectiveFunction;
import org.apache.commons.math.optimization.linear.Relationship;
import org.apache.commons.math.optimization.linear.SimplexSolver;

/**
 * @author esther
 * 
 */
public class ProgLinear {

	public void calcFlux(double[][] matrix) {
		
		//  v1,v2,v3,b1,b2,b3    A,B,C
		
		double[] max = {0,0,0,0,1,0};
		
		LinearObjectiveFunction f = new LinearObjectiveFunction(max, 0);
		
		Collection constraints = new ArrayList();
		for (int i = 0; i < matrix.length; i++) {
			constraints.add(new LinearConstraint(matrix[i], Relationship.EQ, 0));
		}

		double[] b1 = {0,0,0,1,0,0};
		constraints.add(new LinearConstraint(b1, Relationship.GEQ, 0));
		constraints.add(new LinearConstraint(b1, Relationship.LEQ, 10));

		double[] b2 = {0,0,0,0,1,0};
		constraints.add(new LinearConstraint(b2, Relationship.GEQ, 0));
		constraints.add(new LinearConstraint(b2, Relationship.LEQ, 10));
		
		double[] b3 = {0,0,0,0,0,1};
		constraints.add(new LinearConstraint(b3, Relationship.GEQ, 3));
		constraints.add(new LinearConstraint(b3, Relationship.LEQ, 10));
		
		
		// create and run the solver
		RealPointValuePair solution = null;
		try {
			solution = new SimplexSolver().optimize(f, constraints,	GoalType.MAXIMIZE, true);
		} catch (OptimizationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		// get the solution
		
		for (int i = 0; i < solution.getPoint().length; i++) {
			System.out.println(solution.getPoint()[i]);
		}
	
		double min = solution.getValue();
		System.out.println("\n"+min + "\n");

	}

	public static void main(String[] args) {
		Stoichometry s = new Stoichometry();
		String pathName = System.getProperty("user.dir")+"\\small.xml";
		double[][] matrix = s.calculaStoichometry(pathName);
		ProgLinear prog = new ProgLinear();
		prog.calcFlux(matrix);
	}	
}
